package com.sy.springsecu.entity;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name = "tb_users")//实体对应的表   这个类现在不使用
public class User implements Serializable {
	private int id;
	private String name;
	private String pwd;
	private String enabled;
	private Set<Role> roles;//多对多 需要

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "c_username")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "c_password")
	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Column(name = "c_enabled")
	public String getEnabled() {
		return enabled;
	}

	public void setEnabled(String enabled) {
		this.enabled = enabled;
	}

	@ManyToMany(targetEntity = Role.class, fetch = FetchType.EAGER)//配置多对多
	@JoinTable(name = "tb_user_role", joinColumns = @JoinColumn(name = "c_user_id"), inverseJoinColumns = @JoinColumn(name = "c_role_id"))
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	//用实体实现spring security 时 必须  重写equals hashcode
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof User))           
			return false;       
		User unit = (User) o;
		System.out.println(name);
		return name.equals(unit.getName());
	}

	@Override
	public int hashCode() {
		int result = name.hashCode();
		System.out.println(name);
		return result;
	}

}